package com.sl.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.sl.springboot.model.*;
import com.sl.springboot.service.CardService;
import com.sl.springboot.service.UcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class CardController {

    // 依赖注入
    @Autowired
    private CardService cardService;
    @Autowired
    private UcardService ucardService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findCard")
    public String findCard(Integer pageIndex, Integer pageSize,String cardType, Model model,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        String type = (String)session.getAttribute("type");
        Map mp = new HashMap();
        mp.put("cardType",cardType);
        PageInfo<Card> pageList = cardService.findPageInfo(pageIndex,pageSize,mp);
        if(type.equals("02")){
            User u = (User)session.getAttribute("ad");
            mp.put("uid",u.getId());
            List<Ucard> ucards = ucardService.queryFilter(mp);
            if(ucards.size()>=1){
                Ucard ucard = ucards.get(0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowDate_String = sdf.format( new Date() );
                Date nowDate_Date = sdf.parse(nowDate_String);
                Date endDate_Date = sdf.parse(ucard.getTime());
                int compareTo = nowDate_Date.compareTo(endDate_Date);
                if(compareTo == -1){
                    model.addAttribute("flags","0");
                }else{
                    model.addAttribute("flags","1");
                }
            }else{
                model.addAttribute("flags","1");
            }
        }
        model.addAttribute("pageList",pageList);
        return "CardList";
    }

    /**
     * 获取某个卡的订购用户
     */
    @RequestMapping(value = "/getUserAndCard" ,method = RequestMethod.GET)
    @ResponseBody
    public ResultVO addCard(@RequestParam("id") String id) {//{"code": 0,"msg": "","count": 1000,"data": [{}, {}]}
        Map mp = new HashMap();
        mp.put("kid",id);
        List<UcardVO> ucardVOS = ucardService.queryFilter1(mp);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setCount(ucardVOS.size());
        resultVO.setMsg("成功");
        resultVO.setData(ucardVOS);
        return resultVO;
    }



    /**
     * 添加
     */
    @RequestMapping(value = "/addCard" ,method = RequestMethod.POST)
    @ResponseBody
    public String addCard( @RequestBody Card card) {
        int d = cardService.addCard(card);
        return "CardList";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteCard")
    @ResponseBody
    public String deleteCard(String id) {
        int d = cardService.deleteCard(id);
        return "CardList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateCard")
    public String updateCard( Card card) {
        int d = cardService.updateCard(card);
        return "redirect:/findCard";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findCardById")
    public String findCardById(String id,Model model,HttpServletRequest request) {
        Card card= cardService.findCardById(id);
        model.addAttribute("card",card);
        return "CardEdit";
    }


}
